package com.symaster.common.geom;

import com.symaster.common.geom.entity.Size;
import com.symaster.common.image.util.ImageUtil;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;

import java.io.FileInputStream;
import java.util.List;

/**
 * @author yinmiao
 * @date 2022-03-29 18:50
 */
public class AdvancedSynthesizer {

    public static Mat compose(PolygonPanel polygonPanel) {
        Size size = polygonPanel.getSize();
        Mat matBg = new Mat(size.toCvSize(), polygonPanel.getCvType(), new Scalar(255, 255, 255, 255));
        List<PolygonPanelItem> polygonPanelItemList = polygonPanel.getPolygonPanelItemList();

        try {
            for (PolygonPanelItem polygonPanelItem : polygonPanelItemList) {
                Mat mat = null;
                try {
                    FileInputStream fileInputStream = new FileInputStream(polygonPanelItem.getSrcImagePath());
                    mat = ImageUtil.toMat(fileInputStream);
                    ImageUtil.synthesisImage(matBg, polygonPanelItem, polygonPanelItem.getSrcPolygon(), polygonPanelItem.getAngle(), mat, new Scalar(255, 255, 255, 255));
                } finally {
                    ImageUtil.close(mat);
                }
            }
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return matBg;
    }
}
